----------------------------------------------------------------------------------------------------------------------------------
--[[

   

]]--
----------------------------------------------------------------------------------------------------------------------------------
--
    local function GetReplica(self)
        return self.inst.replica.hutao_com_healing_grove or self.inst.replica._.hutao_com_healing_grove
    end
    local function set_current(self, value)
        local replica = GetReplica(self)
        if replica then
            replica:SetCurrent(value)
        end
    end
----------------------------------------------------------------------------------------------------------------------------------
local hutao_com_healing_grove = Class(function(self, inst)
    self.inst = inst

    self.DataTable = {}
    self.TempTable = {}
    self._onload_fns = {}
    self._onsave_fns = {}


    self.current = 0
    self:AddOnLoadFn(function()
        self.current = self:Get("current",0)
    end)
    self:AddOnSaveFn(function()
        self:Set("current",self.current)
    end)
    self.Ready = false
    inst:DoTaskInTime(0.5,function()
        self.Ready = true
    end)

end,
nil,
{
    current = set_current,
})
------------------------------------------------------------------------------------------------------------------------------
-- 储存值
    function hutao_com_healing_grove:DoDelta(value)
        local old = self.current
        self.current = math.max(self.current + value,0)
        self.inst:PushEvent("hutao_com_healing_grove_update",{old = old, new = self.current ,activing = self.current > 0})
    end    
    function hutao_com_healing_grove:GetCurrent()
        return self.current
    end
    function hutao_com_healing_grove:Clear()
        if not self.Ready then return end
        self:DoDelta(-self.current)
    end
------------------------------------------------------------------------------------------------------------------------------
--- 修正值
    function hutao_com_healing_grove:GetHealthFixCostMult() --- 护盾值消耗倍率
        return 1
    end
    function hutao_com_healing_grove:GetHealthDeltaFixed(delta)
        -- delta 是负数，代表伤害值
        if self.current <= 0 then
            return delta
        end        
        local cost_mult = self:GetHealthFixCostMult()        
        -- 考虑消耗倍率调整delta
        local adjusted_delta = delta * cost_mult        
        if self.current > -adjusted_delta then -- 如果当前郁愈域的值大于调整后的伤害值，则全部消耗
            self.current = self.current + adjusted_delta
            return 0
        else -- 如果当前郁愈域的值小于调整后的伤害值，则全部消耗，并返回剩余伤害值
            local ret = self.current + adjusted_delta
            self.current = 0
            return ret - self.current -- 这里确保正确返回剩余未被护盾抵消的伤害值
        end
    end
    function hutao_com_healing_grove:GetSanityFixCostMult() --- 护盾值消耗倍率
        return 2
    end
    function hutao_com_healing_grove:GetSanityDeltaFixed(delta)
        -- delta 是负数，代表伤害值
        if self.current <= 0 then
            return delta
        end        
        local cost_mult = self:GetSanityFixCostMult()        
        -- 考虑消耗倍率调整delta
        local adjusted_delta = delta * cost_mult        
        if self.current > -adjusted_delta then -- 如果当前郁愈域的值大于调整后的伤害值，则全部消耗
            self.current = self.current + adjusted_delta
            return 0
        else -- 如果当前郁愈域的值小于调整后的伤害值，则全部消耗，并返回剩余伤害值
            local ret = self.current + adjusted_delta
            self.current = 0
            return ret - self.current -- 这里确保正确返回剩余未被护盾抵消的伤害值
        end
    end
------------------------------------------------------------------------------------------------------------------------------
---
    function hutao_com_healing_grove:HealthDownBlocked()
        
    end
    function hutao_com_healing_grove:SanityDownBlocked()
        
    end
------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------
----- onload/onsave 函数
    function hutao_com_healing_grove:AddOnLoadFn(fn)
        if type(fn) == "function" then
            table.insert(self._onload_fns, fn)
        end
    end
    function hutao_com_healing_grove:ActiveOnLoadFns()
        for k, temp_fn in pairs(self._onload_fns) do
            temp_fn(self)
        end
    end
    function hutao_com_healing_grove:AddOnSaveFn(fn)
        if type(fn) == "function" then
            table.insert(self._onsave_fns, fn)
        end
    end
    function hutao_com_healing_grove:ActiveOnSaveFns()
        for k, temp_fn in pairs(self._onsave_fns) do
            temp_fn(self)
        end
    end
------------------------------------------------------------------------------------------------------------------------------
----- 数据读取/储存
    function hutao_com_healing_grove:Get(index,default)
        if index then
            return self.DataTable[index] or default
        end
        return nil or default
    end
    function hutao_com_healing_grove:Set(index,theData)
        if index then
            self.DataTable[index] = theData
        end
    end

    function hutao_com_healing_grove:Add(index,num,min,max)
        if index then
            if min and max then
                local ret = (self.DataTable[index] or 0) + ( num or 0 )
                ret = math.clamp(ret,min,max)
                self.DataTable[index] = ret
                return ret
            else
                self.DataTable[index] = (self.DataTable[index] or 0) + ( num or 0 )
                return self.DataTable[index]
            end
        end
        return 0
    end
------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------
    function hutao_com_healing_grove:OnSave()
        self:ActiveOnSaveFns()
        local data =
        {
            DataTable = self.DataTable
        }
        return next(data) ~= nil and data or nil
    end

    function hutao_com_healing_grove:OnLoad(data)
        if data.DataTable then
            self.DataTable = data.DataTable
        end
        self:ActiveOnLoadFns()
    end
------------------------------------------------------------------------------------------------------------------------------
return hutao_com_healing_grove







